import { getUserInfo, toWithdrawal } from '../../../../../common/api/index'
Page({
    data: {
		amount: null,
		userInfo: {},
		userParentInfo: {},
		fileList: [],//收款码
    },
    onLoad(options) {

	},
    onShow() {
		this.getUserInfoData()
    },
	// 上传收款码
	afterRead(event) {
		const { file } = event.detail;
		// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
		wx.uploadFile({
			filePath: file.url,
			name: 'files',
			header:{
				'Authorization': wx.getStorageSync('token')
			},
			url: 'https://www.liyacq.cn/api/common/uploadFile',
			success: (res) => {
				let ress = JSON.parse(res.data)
				let path = ress.data[0].browseUrl
				const { fileList = [] } = path;
				fileList.push({ url: path });
				this.setData({ fileList });
			},
			fail: () => {
			  console.log("上传打款凭证失败...");
			}
		})
	},
	// 输入
	onChange(e){
		let key = e.target.dataset.name;
		this.setData({
			[key]: e.detail
		})
	},
	// 体现历史
	toHistory(){
		wx.navigateTo({
			url: '/pages/component/mine/wallet/history/index'
		})
	},
	// 获取个人信息(余额)
	getUserInfoData(){
		getUserInfo().then(res => {
			if(res.code == 200){
				this.setData({
					userInfo: res.data.user,
					userParentInfo: res.data.userPar,
				})
			}
		})
	},
	// 提交提现
	submitWithdraw(){
		let that = this
		if(!that.data.amount){
			wx.showToast({
				title: '请正确输入提现金额',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(that.data.amount >= that.data.userInfo.balance){
			wx.showToast({
				title: '提现金额不能大于余额',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(that.data.fileList.length == 0){
			wx.showToast({
				title: '请上传收款码',
				icon: 'none',
				duration: 2000
			})
			return
		}
		let data = {
			amount: that.data.amount,
			withdrawUrl: this.data.fileList[0].url,
		}
		wx.showModal({
			title: '提示',
			content: '是否提交提现申请',
			success (res) {
				if (res.confirm) {
					toWithdrawal(data).then(res => {
						if(res.code == 200){
							wx.showToast({
								title: '提现申请成功',
								icon: 'none',
								duration: 2000
							})
							that.setData({
								amount: null,
								fileList: [],
							})
							that.getUserInfoData()
						}
					})
				}
			}
		})
	},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})